/**
 * @file 工单操作-优惠
 * @author yangjiangming <449654337@qq.com>
 * @date 2018-12-10 20:13:16
 */
layui.define(['layer','form','list_modify_common', '$http', 'NP'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var tool = layui.list_modify_common;
    var $http = layui.$http;
    var NP = layui.NP;

    /**
     * 判断是否是正整数且最多小数点后两位;
     * @param val {number/string} 字符串或数字
     * @returns {boolean} 返回是否是数字
     */
    function isNumber(val) {
        var reg = /^[0-9]+([.]{1}[0-9]{1,2})?$/;//正整数到小数点后两位
        return reg.test(val);
    };

	/**
	 * 判断是否是正整数且最多小数点后1位;
	 * @param val {number/string} 字符串或数字
	 * @returns {boolean} 返回是否是数字
	 */
	function isNumber2(val) {
		var reg = /^[0-9]+([.]{1}[0-9]{1,1})?$/;//正整数到小数点后两位
		return reg.test(val);
	};
    /**
     * 验证规则
     */
    function verify() {
        form.verify({
            "user-number":function (val) {
                if(!isNumber(val)){
                    return "请输入正整数且最多两位小数";
                }

                // 非删除标识优惠金额必须大于0
                if (+val < 0) {
                    return "优惠金额必须大于等于0";
                }

                if(val > 1000000 ){
                    return "不能超过一百万"
                }

                if(NP.times(val, 100) > discountVO.maxDiscountAmount){
                    return "不能超过" + Fen2Yuan(discountVO.maxDiscountAmount);
                }
            },
			"user-freediscount-number":function (val) {
				if(!isNumber2(val) && !removeFlag){
					return "请输入正整数且最多1位小数";
				}

				// 非删除标识优惠金额必须大于0
				var removeDom = $("input[name='removeDiscount']")[0];
				var removeFlag = removeDom && removeDom.checked;
				if (+val < 0 && !removeFlag) {
					return "优惠折扣必须大于0";
				}

				if(val > 9.9 && !removeFlag ){
					return "优惠金额不能超过9.9折扣"
				}
			}
        })
    }
    /**
     * 初始化页面
     */
    function initPage () {
    }

    /**
     * 分转元
     * @param num
     * @returns {*}
     * @constructor
     */

    function Fen2Yuan(numstr) {
        Number.prototype.div = function (arg) {
            var t1 = 0, t2 = 0, r1, r2;
            try {
                t1 = this.toString().split(".")[1].length
            } catch (e) {
            }
            try {
                t2 = arg.toString().split(".")[1].length
            } catch (e) {
            }
            with (Math) {
                r1 = Number(this.toString().replace(".", ""));
                r2 = Number(arg.toString().replace(".", ""));
                return (r1 / r2) * pow(10, t2 - t1);
            }
        };
        var num = Number(numstr);
        return (num.div(100)).toFixed(2);
    }

    /**
     * 元转分
     * @param numstr
     * @returns {string}
     * @constructor
     */
    function Yuan2Fen(numstr) {
        var num = Number(numstr)
        return (num * 100).toFixed(0);
    }


    /**
     * 初始化页面事件
     */
    function initEvent () {
        verify();//验证
        form.on("submit()",function (data) {
            var url =  '/work/modify/save/discount.action';

            if (data.field.removeDiscount === undefined) {
                data.field.removeDiscount = '1';
            }
            if (data.field.removeDiscount === "2") {
                data.field.discountAmount = '0';
            }
            data.field.discountAmount = Yuan2Fen(data.field.discountAmount);

            $http.lockSave(url, data.field, data.elem).then(function () {
                layer.msg('操作成功', {icon: 1, time: 1000}, function () {
                    tool.goOperateHome();
                });
            }, function (msg) {
                if (msg.indexOf("是否核销卡券") > 0){
                    layer.confirm(msg, {
                        btn: ['确认','取消'], title: '确认核销？'
                    }, function (index) {
                        data.field.forceVerify = 2;
                        $http.lockSave(url, data.field, data.elem).then(function () {
                            layer.msg('操作成功', {icon: 1, time: 1000}, function () {
                                tool.goOperateHome();
                            });
                        }, function (msg) {
                            layer.msg(msg);
                        });
                    }, function (index) {
                        layer.close(index);
                    });
                } else {
                    layer.msg(msg);
                }
            });

            return false
        });

		// 订单类型单选框的选中事件处理器
		form.on('radio(disCategory)', function (data) {
			$('#dvDiscountAmt').hide();
			$("input[name='discountAmount']").attr("lay-verify","");
			$('#dvCoupon').hide();
			$("input[name='couponCode']").attr("lay-verify","");
			if (data.value == 1) {
				$("input[name='discountAmount']").attr("lay-verify","required|user-number");
				$('#dvDiscountAmt').show();
                $("#couponCode").val("");
			} else {
				$("input[name='couponCode']").attr("lay-verify","required");
				$('#dvCoupon').show();
                $("#discountAmount").val("");
			}
		});
    }

    // 程序入口
    function init () {

      // 初始化页面
      initPage();

      // 初始化事件
      initEvent();

    }


    // 初始化
    init();

    exports('oms_work_modify_discount');
});
